﻿//     Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Gateway.Configuration;

namespace Microsoft.Gateway.RulesConfigurator.ConfigComparitors
{
    class AuthZComparitor : IEqualityComparer<RulesConfiguration>
    {
        public bool Equals(RulesConfiguration lhs, RulesConfiguration rhs)
        {
            return lhs.SafeEquals(rhs, (x, y) =>
                String.Equals(lhs.Directory, rhs.Directory, StringComparison.OrdinalIgnoreCase) &&
                String.Equals(lhs.ApplicationName, rhs.ApplicationName, StringComparison.OrdinalIgnoreCase) &&
                lhs.Roles.Count == rhs.Roles.Count &&
                lhs.Roles.Keys
                    .OrderBy(key => key, StringComparer.OrdinalIgnoreCase)
                    .SequenceEqual(y.Roles.Keys.OrderBy(key => key, StringComparer.OrdinalIgnoreCase)) &&
                lhs.Roles
                    .OrderBy(role => role.Key, StringComparer.OrdinalIgnoreCase)
                    .Select(role => role.Value)
                    .SequenceEqual(rhs.Roles
                            .OrderBy(role => role.Key, StringComparer.OrdinalIgnoreCase)
                            .Select(role => role.Value),
                        new RoleAuthZComparitor()));
        }

        public int GetHashCode(RulesConfiguration obj)
        {
            throw new NotImplementedException();
        }

        class RoleAuthZComparitor : IEqualityComparer<Role>
        {
            public bool Equals(Role lhs, Role rhs)
            {
                return lhs.RequireAuthenticationBelow.SequenceEqual(rhs.RequireAuthenticationBelow);
            }

            public int GetHashCode(Role obj)
            {
                throw new NotImplementedException();
            }
        }
    }
}
